<template>
  <div>
    <!-- 筛选项 -->
    <el-row :gutter="20" class="gl_searchBox">
      <el-col :md="6" :lg="4">
        <p>排序方式</p>
        <el-select v-model="PointRankListQO.DistrictType" size="mini" placeholder="请选择" @change="getPageList()">
          <el-option v-for="item in dict.districtType" :key="item.code" :value="item.code" :label="item.value" />
        </el-select>
      </el-col>
      <el-col :md="6" :lg="4">
        <p>按项目选择</p>
        <el-select v-model="PointRankListQO.CategoryId" size="mini" placeholder="请选择" @change="getPageList()">
          <el-option label="全部" value="" />
          <el-option v-for="(data, index) in eventType" :key="index" :label="data.name" :value="data.id" />
        </el-select>
      </el-col>
      <el-col :span="6" :lg="4">
        <p>区域名称</p>
        <el-input v-model="PointRankListQO.Keyword" placeholder="请输入名称" size="mini" clearable @change="getPageList()" />
      </el-col>
      <el-col :span="8" :lg="6" align="left" class="search_btn">
        <!-- 查询按钮 -->
        <el-button type="primary" size="mini" @click="getPageList()">{{ $t('button.query') }}</el-button>
        <el-button :loading="downloadTsLoading" style="margin-top: 27px" type="warning" size="mini" @click="downloadPDF()">导出积分表</el-button>
      </el-col>
    </el-row>
    <!-- 赛事列表 -->
    <div class="gl_mb_15">
      <el-table v-loading="tableLoading" class="gl_mt_15  game_table_list" :data="tableData" tooltip-effect="dark" style="width: 100%" size="mini">
        <el-table-column prop="finalRank" align="center" label="排名" :width="120" />
        <el-table-column label="名称" :min-width="200">
          <template slot-scope="{ row }">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="金牌" align="center">
          <template slot-scope="{ row }">
            <span>{{ row._subRankList._1 && row._subRankList._1.subRankCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="银牌" align="center">
          <template slot-scope="{ row }">
            <span>{{ row._subRankList._2 && row._subRankList._2.subRankCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="铜牌" align="center">
          <template slot-scope="{ row }">
            <span>{{ row._subRankList._3 && row._subRankList._3.subRankCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="第五名" align="center">
          <template slot-scope="{ row }">
            <span>{{ row._subRankList._5 && row._subRankList._5.subRankCount }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="finalPoints" align="center" label="总分" />
      </el-table>
    </div>
    <el-row style="text-align: right;" class="gl_pb_15">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="PointRankListQO.MaxResultCount"
        :current-page="PointRankListQO.Page"
        @size-change="changePageSize"
        @current-change="changePage"
      />
    </el-row>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex';
import { api_GET_DownLoadFileAddHeader } from '@/extend/api/file';
import { errorText } from '@/lib';
import defaultGameLogo from '@/assets/images/game/game_logoDefaultPhoto.png';
import { Api_GET_EventsManagementEventResultUnifiedPointRankListQO } from '@/lib/form/game/swaggerForm';
import {
  api_GET_EventsManagementCategory,
  api_GET_EventsManagementEventResultUnifiedPointRankList
} from '@/extend/api/gameswaggerApi/app.js';

export default {
  props: {
    rankType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      defaultGameLogo: defaultGameLogo,
      PointRankListQO: new Api_GET_EventsManagementEventResultUnifiedPointRankListQO(),
      tableData: [],
      eventType: [],
      downloadTsLoading: false,
      total: 0,
      buLoading: false,
      tableLoading: false,
      gameId: ''
    };
  },
  computed: {
    ...mapState({
      dict: state => state.dict.dict
    })
  },
  created() {
    this.getDict();
    this.initForm();
  },
  methods: {
    ...mapActions({ getDict: 'dict/getDict' }),
    // 初始化
    initForm() {
      console.log(this.rankType);
      this.gameId = this.$route.params.parentId;
      this.PointRankListQO = new Api_GET_EventsManagementEventResultUnifiedPointRankListQO();
      this.PointRankListQO.CategoryId = '';
      this.PointRankListQO.DistrictType = this.dict.districtType[0].code;
      this.getCategory();
    },
    // 获取项目列表
    getCategory() {
      var _this = this;
      const params = {
        CompetitionId: this.gameId,
        EventTypeCode: ''
      };
      if (this.rankType === 'All') {
        params.EventTypeCode = '';
      }
      if (this.rankType === 'Competitive') {
        params.EventTypeCode = 'Competitive';
      }
      if (this.rankType === 'NoneCompetitive') {
        params.EventTypeCode = 'NoneCompetitive';
      }
      api_GET_EventsManagementCategory(params).then(res => {
        _this.eventType = res.items;
        _this.getPageList();
      }).catch(() => {
        _this.$message({ type: 'error', message: '获取项目列表失败！' });
      });
    },
    // 获取赛事列表
    getPageList(page) {
      if (this.rankType === 'All') {
        this.PointRankListQO.EventTypeCode = '';
      }
      if (this.rankType === 'Competitive') {
        this.PointRankListQO.EventTypeCode = 'Competitive';
      }
      if (this.rankType === 'NoneCompetitive') {
        this.PointRankListQO.EventTypeCode = 'NoneCompetitive';
      }
      this.PointRankListQO.CompetitionId = this.gameId;
      this.PointRankListQO.Type = 'District';
      this.PointRankListQO.IncludeFinal = true;
      this.PointRankListQO.IncludeSubRank = true;
      this.PointRankListQO.Page = page || 1;
      this.PointRankListQO.SkipCount = (this.PointRankListQO.Page - 1) * this.PointRankListQO.MaxResultCount;
      this.tableLoading = true;
      api_GET_EventsManagementEventResultUnifiedPointRankList(this.PointRankListQO).then(res => {
        var returnData = res.items;
        // 处理返回的数据
        this.tableData = this.hanldReault(returnData);
        this.total = res.totalCount;
        this.tableLoading = false;
      }, error => {
        this.tableLoading = false;
        const _msg = error.error.message || error.message;
        this.$message({ type: 'error', message: _msg });
      });
    },
    // 处理成绩
    hanldReault(eventResult) {
      const returnEventResult = [];
      for (let i = 0; i < eventResult.length; i++) {
        const item = eventResult[i];
        item._subRankList = this.hanldRank(item.subRankList);
        returnEventResult.push(item);
      }
      return returnEventResult;
    },
    // 处理排名
    hanldRank(subRankList) {
      const returnRankInfo = {};
      for (let i = 0; i < subRankList.length; i++) {
        const item = subRankList[i];
        returnRankInfo['_' + item.subRank] = item;
      }
      return returnRankInfo;
    },
    // 获取静态文件
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    errorPhoto() {
      const img = event.srcElement;
      img.src = this.defaultGameLogo;
    },
    downloadPDF() {
      var _this = this;
      this.downloadTsLoading = true;
      const tempUrl = `/api/excel`;
      const fileName = '积分表' + '.xlsx';
      let CategoryId = '';
      let CategoryIds = '';
      if (this.PointRankListQO.CategoryId !== '') {
        CategoryId = this.PointRankListQO.CategoryId;
      } else {
        for (let i = 0; i < this.eventType.length; i++) {
          const item = this.eventType[i].id;
          CategoryIds = CategoryIds + item;
          if (i !== this.eventType.length - 1) {
            CategoryIds = CategoryIds + ',';
          }
        }
      }
      const params = {};
      params.ReportName = 'LeagueRank';
      params.Type = this.PointRankListQO.Type;
      params.CompetitionId = this.gameId;
      params.CategoryId = CategoryId;
      params.CategoryIds = CategoryIds;
      params.EventTypeCode = this.PointRankListQO.EventTypeCode;
      params.DistrictType = this.PointRankListQO.DistrictType;
      params.Keyword = this.PointRankListQO.Keyword;
      api_GET_DownLoadFileAddHeader(tempUrl, fileName, params).then(res => {
        _this.downloadTsLoading = false;
      }).catch(error => {
        _this.downloadTsLoading = false;
        errorText(error).then(function(text) {
          _this.$message({ type: 'error', message: text });
        });
      });
    },
    changePage(page) {
      this.getPageList(page);
    },
    changePageSize(pagesize) {
      this.PointRankListQO.MaxResultCount = pagesize;
      this.getPageList();
    }
  }
};
</script>
<style lang="scss" scoped>
  .statistics_title{
    line-height: 50px;
  }
  .statistics_label{
    text-align: right;
    font-weight: bold;
  }
  .statistics_nums{
    font-size: 40px;
    color: rgb(58, 128, 236);
    margin-bottom: 10px;
  }
  .game_table_list .game_coverimg {
    width: 74px;
    height: 74px;
    border-radius: 50%;
    vertical-align: middle;
  }
</style>
